﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using WindowsFormsApplication1.DTO;

namespace WindowsFormsApplication1.DAO
{
    class ChiTietCoiThiDAO
    {
        public static bool Insert(ChiTietCoiThiDTO ctctDTO)
        {
            bool check = true;
            int k = checkDaTa(ctctDTO);
            SqlConnection ketNoi = AbstractDAO.Connection();
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = ketNoi;

            if (k != 1)
            {
                cmd.CommandText = @"INSERT INTO ChamCongDB.dbo.CHITIETCOITHI (ID_BienBan, ID_GiaoVien, SoBaiThu)
                    VALUES (" + ctctDTO.id_bienban + ", '" + ctctDTO.id_giaovien1 + "', " + ctctDTO.soBaiThu1 + ")";
                if (cmd.ExecuteNonQuery() == -1)
                    check = false;
            }

            if (k != 2)
            {
                cmd.CommandText = @"INSERT INTO ChamCongDB.dbo.CHITIETCOITHI (ID_BienBan, ID_GiaoVien, SoBaiThu)
                VALUES (" + ctctDTO.id_bienban + ", '" + ctctDTO.id_giaovien2 + "', " + ctctDTO.soBaiThu2 + ")";
                if (cmd.ExecuteNonQuery() == -1)
                    check = false;
            }
            if (check == false)
                return false;
            else
                return true;
        }
        private static int checkDaTa(ChiTietCoiThiDTO ctctDTO)
        {
            int k = 0;
            if ((ctctDTO.id_giaovien1 == null) || (ctctDTO.soBaiThu1 == null))
                k = 1;
            if ((ctctDTO.id_giaovien2 == null) || (ctctDTO.soBaiThu2 == null))
                k = 2;
            return k;
        }
    }
}
